import {JSEncrypt} from "jsencrypt";
// 密钥对生成 http://web.chacuo.net/netrsakeypair

const apiPublicKey = import.meta.env.VITE_APP_RSA_PUBLIC_KEY;

// 前端不建议存放私钥 不建议解密数据 因为都是透明的意义不大
const apiPrivateKey = '*****************'


const publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCacr+XR6hdrRislUAxCbzypCBL\n' +
    'pkvpQHQFvAFAa8BEtPqhJwDgdWI4TEVfUjReB5lG1+G5x3H+w1xB5o0vipurbEXf\n' +
    'XPJWwWwLy2T0IQ9Q0cJIYuUAS0XdMCq5H9xXYssbjPQrnRzovWPY1NO1UX4+0mLF\n' +
    'U27mGYM4rf1+9igvmwIDAQAB';

const privateKey = 'MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJpyv5dHqF2tGKyV\n' +
    'QDEJvPKkIEumS+lAdAW8AUBrwES0+qEnAOB1YjhMRV9SNF4HmUbX4bnHcf7DXEHm\n' +
    'jS+Km6tsRd9c8lbBbAvLZPQhD1DRwkhi5QBLRd0wKrkf3FdiyxuM9CudHOi9Y9jU\n' +
    '07VRfj7SYsVTbuYZgzit/X72KC+bAgMBAAECgYACbK3rrTxML10Ly7jOP3dJd9O2\n' +
    'wrPxkKMObZa3cPmVTbubFaAPf5UJigKTLhjHjeb7rbflarQanVNKO/Kx14/9Ndco\n' +
    '3O3cVec17/2NLxYohiymLV8xnUYVeTPFuzsr/B3QtRZjWaXFFDyR/JQF02OXg14d\n' +
    '0gVqn1xciwkVwP7BAQJBANx/u+NF+v5mKH1sc6WHXJgLvq3gW+qWvU0Sjv/ic9m5\n' +
    'V6Oy3PvI4PfcI8L37RLduf1SfwJ/Wbbl9SNGOIVhBgsCQQCzUJ0VQ1WmRW4NzJ0w\n' +
    'P5ctrRgiXOHF4pizAiJpAgvsjZQ1hhNMpSsat+zrvCEswaNZb4f5Vk5IMTHGoMiC\n' +
    'xEaxAkEAw3Cwr/TyURVppCpBoyGEz/vxChtIPXlY1vyxJVuqBfzDEoTSXJYj2W4V\n' +
    'gJXSW5uOZFv/2c3ZTkUrcBWouYhTGwJBAJlaJ/03SNiaKDTE+Ol6eOAnWKyKosuJ\n' +
    'ru3otWT4yxnTOx1LPNYfq21+PrCchAKy1/B04eTrWqeC6v7kIpczJQECQD+xkpVF\n' +
    'E00F6rFt0oVYLMDS+xJstU+QEJN4WhEE6gtKoqvkYe2jkMk8fMdlllKuVqgLWYCw\n' +
    'zvqw8XREVPPYTdM='

// 加密
export const apiEncrypt = (txt: any) => {
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(apiPublicKey); // 设置公钥
    return encryptor.encrypt(txt); // 对数据进行加密
};

// 解密
export const apiDecrypt = (txt: any) => {
    const encryptor = new JSEncrypt();
    encryptor.setPrivateKey(apiPrivateKey); // 设置私钥
    return encryptor.decrypt(txt); // 对数据进行解密
};


//下面两个方法针对返回用户数据加密存储cookie
// 加密
export const encrypt = (data: any) => {
    if (typeof data === "object") {
        try {
            data = JSON.stringify(data);
        } catch (error) {
            console.log("encrypt error:", error);
        }
    }
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(publicKey); // 设置公钥

    const chunkSize = 100; // 分段大小
    const encryptedChunks = []; // 存储分段加密后的数据

    for (let i = 0, len = data.length; i < len; i += chunkSize) {
        const chunk = data.substring(i, i + chunkSize);
        const encryptedChunk = encryptor.encrypt(chunk);
        encryptedChunks.push(encryptedChunk);
    }

    return encryptedChunks;
};

// 分段解密
export const decrypt = (data: any) => {
    const encryptor = new JSEncrypt();
    encryptor.setPrivateKey(privateKey); // 设置私钥

    const decryptedChunks = []; // 存储分段解密后的数据

    for (let i = 0, len = data.length; i < len; i++) {
        const decryptedChunk = encryptor.decrypt(data[i]);
        decryptedChunks.push(decryptedChunk);
    }

    let decryptedData = decryptedChunks.join(''); // 合并解密后的分段数据

    // 去除填充部分
    const paddingIndex = decryptedData.indexOf('\0');
    if (paddingIndex !== -1) {
        decryptedData = decryptedData.substring(0, paddingIndex);
    }

    return decryptedData;
};
